#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

//char *newnew = (char*)"myVAL=666666";

char *const addenv[] = {
     (char *const)"MYVAL=123456",
     (char *const)"MYVAL1=123456",
     NULL
};

int main()
{
    printf("我的程序要开始运行了!\n");
    //execl("/usr/bin/ls","ls","-l","-a",NULL);
    if(fork() == 0)
    {
        printf("I am Child,my pid is: %d\n",getpid());
        sleep(1);
        //execl("./other","other",NULL);
       // exit(1);   
        
        char *const argv[] = {
            (char*const)"other",
            (char*const)"-a",
            (char*const)"-b",
            (char*const)"-c",
            (char*const)"-d",
            NULL
        };

        for(int i = 0;addenv[i];i++)
        {
            putenv(addenv[i]);
        }
        //execv("/usr/bin/ls", argv);
        //putenv(newnew);
        //execvp("./other",argv);
        extern char **environ;
        execvpe("./other",argv,environ);
        exit(1);
    }
    waitpid(-1,NULL,0);
    printf("我的程序运行完毕\n");
    //exit(1);
    //return 0;
}

//int main()
//{
//    pid_t id = fork();
//    if(id == 0)
//    {
//        //子
//        int cnt = 2;
//        while(1)
//        {
//            printf("我是一个子进程, pid: %d, ppid: %d\n", getpid(), getppid());
//            sleep(1);
//            cnt--;
//            int *p = 0;
//            *p = 100;
//        }
//        exit(1);
//    }
//    //sleep(10);
//    int status = 0;
//    pid_t rid = waitpid(id, &status, 0);
//    if(rid > 0)
//    {
//        printf("wait success, rid: %d, exit information: %d, exit sigal: %d\n", rid, (status>>8)&0xFF, status&0x7F);
//    }
//    else
//    {
//         printf("wait failed: %d: %s\n", errno, strerror(errno));
//    }
//    //sleep(10);
//    
//    return 0;
//}
//
//
////void fun()
////{
//    printf("fun begin!\n");
//    _exit(20);
//    printf("fun end!\n");
//}
//int main()
//{
//    fun();
//    printf("main!\n");

    //exit(16);

    //int a = 10;

    //a /= 0;

    //return 66;


    //int i = 0;
    //for(;i<100;i++)
    //{
    //    printf("%d->%s\n",i,strerror(i));
    //}



    //FILE *fp = fopen("log.txt","r");
    //if(fp == NULL) return 13;

    ////读取
    //fclose(fp);
    //return 0;
//}
